<?php

namespace app\common\model\hotel;


use app\api\model\hotel\HotelThirdOrder;
use app\common\model\hotel\Hotel as HotelModel;
/**
 *
 */
class Order
{

    public $url = 'https://open-partner.miot.com.cn/api';

    //验单
    public function order_check($params)
    {
        $url = $this->url.'/v1/channel/order_check';

        $hotel = new HotelModel();

        $token = $hotel->get_token();

        $hotel_id = $params['hotel_id'];

        $checkin_date = $params['checkin_date'];

        $checkout_date = $params['checkout_date'];

        $guest_name = $params['guest_name'];

        $phone = $params['phone'];

        $remark = $params['remark'];

        $room_type_code = [
            $params['room_type_code']
        ];
        //$room_type_code = $params['room_type_code'];
        //$room_type_code = json_decode($room_type_code,true)?json_decode($room_type_code,true):_json_decode($room_type_code);
        //$room_code_list = $params['room_code_list'];

        $room_num = $params['room_num'];

        $param = [
            'hotel_id'=>$hotel_id,
            'checkin_date'=>$checkin_date,
            'checkout_date'=>$checkout_date,
            'guest_name'=>$guest_name,
            'phone'=>$phone,
            'remark'=>$remark,
            'room_type_code'=>$room_type_code,
            //'room_code_list'=>$room_code_list,
            'room_num'=>$room_num,
        ];

        $data = json_encode($param,320);

        $res = https_request($url,$data,$token,'POST');

        $result = json_decode($res,true);

        if ($result['code']==200){
            return [
                'code'=>1,
                'data'=>$result['data'],
                'msg'=>'success'
            ];
            //return $result['data'];
        } else {
            return [
                'code'=>0,
                'msg'=>$result['message']
            ];
            //throw new \Exception($result['message']);
        }
    }

    //提交订单
    /*public function ordercreate($params)
    {
        if (empty($params['guest_name'])){
            throw new \Exception('请输入客人姓名');
        }

        if (empty($params['phone'])){
            throw new \Exception('请输入客人电话');
        }

        $room_type_code = $params['room_type_code'];
        $room_type_code = json_decode($room_type_code,true)?json_decode($room_type_code,true):_json_decode($room_type_code);


        $order_model = new HotelThirdOrder();
        $order_res = $order_model->ordercreate($params,$room_type_code);

        return $order_res;
    }*/

    //第三方提交订单
    public function order_commit($order_id)
    {
        $url = $this->url.'/v1/channel/order_commit';

        $hotel = new HotelModel();

        $token = $hotel->get_token();

        $order_info = HotelThirdOrder::get($order_id);
        if (!$order_info) throw new \Exception('订单不存在');
        //$room_type_code = $params['room_type_code'];
        //$room_type_code = json_decode($room_type_code,true)?json_decode($room_type_code,true):_json_decode($room_type_code);

        $param = [
            'hotel_id'=>$order_info['hotel_id'],
            'checkin_date'=>$order_info['checkin_date'],
            'checkout_date'=>$order_info['checkout_date'],
            'guest_name'=>$order_info['guest_name'],
            'phone'=>$order_info['phone'],
            'remark'=>$order_info['remark'],
            'ota_order_id'=>$order_info['order_no'],
            //'room_type_code'=>json_decode($order_info['room_type_code'],true),
            //'room_code_list'=>$room_code_list,
            'room_num'=>$order_info['room_num'],
            'member_total_price'=>$order_info['pay_money']*100
        ];

        $param['room_type_code'] = [
            $order_info['room_type_code']
        ];

        $data = json_encode($param,320);

        $res = https_request($url,$data,$token,'POST');

        $result = json_decode($res,true);
        //dump($result);die;
        if ($result['code']==200){
            $update_data = [
                'parent_order_id'=>$result['data']['parent_order_id'],
                'son_order_id'=>$result['data']['son_order_id_list'][0],
                'third_order_status'=>'reserve'
            ];
            HotelThirdOrder::where(['order_id'=>$order_id])
                ->update($update_data);
            return $result['data'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //取消订单
    public function order_cancel($hotel_id,$ota_order_id)
    {
        $url = $this->url.'/v1/channel/order_cancel';

        $hotel = new HotelModel();

        $token = $hotel->get_token();

        $param = [
            'hotel_id'=>$hotel_id,
            'ota_order_id'=>$ota_order_id,
        ];

        $data = json_encode($param,320);

        $res = https_request($url,$data,$token,'POST');

        $result = json_decode($res,true);

        if ($result['code']==200){
            return [
                'code'=>1,
                'data'=>$result['data'],
                'msg'=>'success'
            ];
            //return $result['data'];
        } else {
            return [
                'code'=>0,
                'data'=>[],
                'msg'=>$result['message']
            ];
            //throw new \Exception($result['message']);
        }
    }

    //办理⼊住
    public function order_checkin($hotel_id,$order_id,$son_order_id_list,$guest,$other_guest)
    {
        $url = $this->url.'/v1/channel/new/order_checkin';

        //$son_order_id_list = json_decode($son_order_id_list,true)?json_decode($son_order_id_list,true):_json_decode($son_order_id_list);

        $hotel = new HotelModel();

        $token = $hotel->get_token();

        $param = [
            'hotel_id'=>$hotel_id,
            'order_id'=>(string)$order_id,
            'son_order_id_list'=>$son_order_id_list,
            'guest'=>$guest,
        ];

        if ($other_guest){
            $other_guest = json_decode($other_guest,true);

            if (count($other_guest)>0){
                $new_other_guest = [];
                foreach ($other_guest as $k=>$v){
                    $new_other_guest[] = [
                        'guest_name'=>$v
                    ];
                }
                $param['other_guest']  = $new_other_guest;
            }

        }

        $data = json_encode($param,320);

        $res = https_request($url,$data,$token,'POST');

        $result = json_decode($res,true);

        if ($result['code']==200){
            return [
                'code'=>1,
                'data'=>$result['data'],
                'msg'=>'success'
            ];
            //return $result['data'];
        } else {
            return [
                'code'=>0,
                'data'=>[],
                'msg'=>$result['message']
            ];
            //throw new \Exception($result['message']);
        }
    }

    //办理续住
    public function stay_over($hotel_id,$parent_order_id,$order_list)
    {
        $url = $this->url.'/v1/channel/order/stay_over';

        $order_list = json_decode($order_list,true)?json_decode($order_list,true):_json_decode($order_list);

        $hotel = new HotelModel();

        $token = $hotel->get_token();

        $param = [
            'hotel_id'=>$hotel_id,
            'parent_order_id'=>$parent_order_id,
            'order_list'=>$order_list
        ];

        $data = json_encode($param,320);

        $res = https_request($url,$data,$token,'POST');

        $result = json_decode($res,true);

        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //办理退房
    public function order_checkout($hotel_id,$ota_order_id)
    {
        $url = $this->url.'/v1/channel/new/order_checkout';
        $hotel = new HotelModel();

        $token = $hotel->get_token();

        $param = [
            'hotel_id'=>$hotel_id,
            'ota_order_id'=>$ota_order_id,
        ];

        $data = json_encode($param,320);

        $res = https_request($url,$data,$token,'POST');

        $result = json_decode($res,true);

        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //删除订单
    public function delete_order($hotel_id,$son_order_id,$parent_order_id)
    {
        $url = $this->url.'/v1/channel/order/delete_order';
        $hotel = new HotelModel();

        $token = $hotel->get_token();

        $param = [
            'hotel_id'=>$hotel_id,
            'son_order_id'=>$son_order_id,
            'parent_order_id'=>$parent_order_id
        ];

        $data = json_encode($param,320);

        $res = https_request($url,$data,$token,'POST');

        $result = json_decode($res,true);

        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //获取云掌柜订单详情
    public function order_detail($hotel_id,$parent_order_id)
    {
        $url = $this->url.'/v1/channel/query/order_detail';
        $hotel = new HotelModel();

        $token = $hotel->get_token();

        $param = [
            'hotel_id'=>$hotel_id,
            'parent_order_id'=>$parent_order_id,
        ];

        $data = json_encode($param,320);

        $res = https_request($url,$data,$token,'POST');

        $result = json_decode($res,true);

        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['message']);
        }

    }

}